<template>
	<view class="community">
		<my-info :comment_count="comment_count"></my-info>
		<view class="card">
			<tabs :titles="['全部讨论','我的讨论']" @itemClick="itemClick"></tabs>
		</view>
		<comment :commentList='commentList' @support="support"></comment>
		<view class="circle" @tap="toAddComment">
			<u-icon name="plus" size="30"></u-icon>
		</view>
		<u-loadmore :status="loadingText.status" :loading-text="loadingText.inLoading"
			:loadmore-text="loadingText.beforeLoading" :nomore-text="loadingText.nomoreText" :height="40" />
	</view>
</template>

<script>
	import {
		get_comment_count,
		get_comment,
		is_support,
		del_comment
	} from '@/api/contents/comment/comment.js'
	import {
		getUserInfo
	} from '@/api/contents/user/user.js'
	import myInfo from '@/components/profile/community/my-info/my-info.vue'
	import tabs from '@/components/common/tabs/tabs.vue'
	import comment from '@/components/profile/community/comment/comment.vue'
	export default {
		components: {
			myInfo,
			tabs,
			comment
		},
		data() {
			return {
				comment_count: {}, //社区消息总数
				commentList: [], //评论列表
				comment_params: {
					is_me: 0,
					limit: 5,
					page: 1
				},
				loadingText: {
					inLoading: '努力加载中',
					beforeLoading: '上拉加载更多',
					nomoreText: '没有更多了',
					status: 'loadmore'
				}

			};
		},
		methods: {
			toAddComment() {
				uni.navigateTo({
					url: '../../../pages/profile/community/addComment/addComment',
				}, )

			},
			// 获取用户信息
			getUserInfo() {
				getUserInfo().then(res => {
					if (res.code == 1) {
						this.$store.commit('updateUserInfo', res.data[0]);
					}
				})
			},
			// 获取消息总数
			get_comment_count() {
				get_comment_count().then(res => {
					if (res.code == 1) {
						this.comment_count = res.data;
					}
				})
			},
			// 获取社区评论
			get_comment() {
				get_comment(this.comment_params).then(res => {
					if (res.code == 1) {
						if (res.data.length) {
							this.commentList = [...this.commentList, ...res.data]
							this.loadingText.status = 'loadmore';
						} else {
							this.loadingText.status = 'nomore'
						}
					}
				})
			},
			// 社区评论点赞
			support(comment_id) {
				is_support({comment_id}).then(res => {
					if(res.code==1){
						uni.showToast({
							icon:'none',
							title:res.msg
						})
						this.commentList.forEach(item=>{
							if(item.comment_id==comment_id){
								if(item.is_support){
									this.$set(item,'is_support',0)
									this.$set(item,'support_count',--item.support_count)
								}else{
									this.$set(item,'is_support',1)
									this.$set(item,'support_count',++item.support_count)
								}
							}
						})
					}

				})
			},
			// 切换全部评论-我的评论
			itemClick(curr) {
				this.commentList=[];
				this.comment_params.page=1;
				this.comment_params.is_me = curr;
				this.get_comment()
			}
		},
		onReachBottom() {
			this.loadingText.status = 'loading';
			this.comment_params.page++;
			this.get_comment();
		},
		onShow() {
			this.commentList=[]
			this.getUserInfo();
			this.get_comment_count();
			this.get_comment();
		},
	}
</script>

<style lang="scss" scoped>
	.community {
		position: relative;
		// #ifdef H5
		height: calc(100vh - 50px);
		//  #endif
		// #ifdef MP-WEIXIN
		height: 100vh;

		// #endif
		// background-color: #F5F7FA;
		.circle {
			background: #fff;
			position: fixed;
			right: 20upx;
			bottom: 150upx;
			width: 100upx;
			height: 100upx;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 50%;
			box-shadow: 0px 1px 12px 0px rgba(62, 62, 62, 0.04);
		}

	}
</style>
